
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.util.ArrayList;

import com.jme.bounding.BoundingBox;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Spatial;
import com.jme.scene.Text;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.MaterialState.ColorMaterial;
import com.jme.system.DisplaySystem;
import com.jme.util.export.binary.BinaryImporter;
import com.jme.util.resource.ResourceLocatorTool;
import com.jme.util.resource.SimpleResourceLocator;
import com.jmex.game.state.CameraGameState;
import com.jmex.game.state.GameState;
import com.jmex.game.state.GameStateManager;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.ObjToJme;

public class CtrlState extends CameraGameState {

	private DisplaySystem display;
	private Text menu1=Text.createDefaultTextLabel( "info" );
	private Text menu2=Text.createDefaultTextLabel( "info" );
	private Text menu3=Text.createDefaultTextLabel( "info" );
	private Text menu4=Text.createDefaultTextLabel( "info" );
	private Text menu5=Text.createDefaultTextLabel( "info" );
	private Spatial title,bgnd;
	private ArrayList <Text>menus;
	private int selected;

	public CtrlState(String name) {
		super(name);
		display = DisplaySystem.getDisplaySystem();
		initInput();

		try {
			// URL folder= MenuState.class.getClassLoader().getResource("data/");
			URL folder= MenuState.class.getClassLoader().getResource("data/");
			ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE,
					new SimpleResourceLocator(folder));     
			URL model1 = MenuState.class.getClassLoader().getResource("data/3d_title.obj");
			URL model2 = MenuState.class.getClassLoader().getResource("data/bgnd.obj");
			FormatConverter converter=new ObjToJme();
			converter.setProperty("mtllib", folder);
			converter.setProperty("texdir", folder);
			ByteArrayOutputStream BO2=new ByteArrayOutputStream();

			converter.convert(model2.openStream(), BO2);
			bgnd=(Spatial) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO2.toByteArray()));
			bgnd.setModelBound(new BoundingBox());
			bgnd.updateModelBound();
			bgnd.setLocalScale(15f);
			bgnd.setLocalTranslation(-4, -28, 70);
			//bgnd.getLocalRotation().set(new Quaternion()
			//	.fromAngleAxis( - FastMath.PI/2, new Vector3f(0,1,0)) );
			rootNode.attachChild(bgnd);
			rootNode.updateGeometricState(0, true);
			rootNode.updateRenderState();			

			ByteArrayOutputStream BO1=new ByteArrayOutputStream();
			converter.convert(model1.openStream(), BO1);
			title=(Spatial) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO1.toByteArray()));
			title.setModelBound(new BoundingBox());
			title.updateModelBound();
			title.setLocalScale(.2f);
			title.setLocalTranslation(0, 2, 10);
			title.getLocalRotation().set(new Quaternion()
			.fromAngleAxis(FastMath.PI, new Vector3f(0,1,0)) );
			rootNode.attachChild(title);
			rootNode.updateGeometricState(0, true);
			rootNode.updateRenderState();		


		} catch (Exception e) {
			System.out.println("Damn exceptions! O_o nyaa\n" + e);
			e.printStackTrace();
			System.exit(0);
		}
		/*
        light = new PointLight();
        light.setDiffuse( new ColorRGBA( 255f, 255f, 255f, 0f ) );
        light.setAmbient( new ColorRGBA( 200f, 200f, 0.5f, 1.0f ) );

        light.setLocation( new Vector3f( -4, -28, 68));
        light.setEnabled( true );
        lightState = display.getRenderer().createLightState();
        lightState.setEnabled( true );
        lightState.attach( light );
        rootNode.setRenderState( lightState );
		 */

		MaterialState ms = display.getRenderer().createMaterialState();
		ms.setColorMaterial(ColorMaterial.Emissive);		
		rootNode.setRenderState(ms);
		rootNode.updateGeometricState(0, true);
		rootNode.updateRenderState();	
	}

	public void onActivate() {
		display.setTitle("Dungeon 3D - Control screen");        
		super.onActivate();
	}

	protected void initInput() {
		
		KeyBindingManager.getKeyBindingManager().set("up", KeyInput.KEY_UP);
		KeyBindingManager.getKeyBindingManager().set("down", KeyInput.KEY_DOWN);
		KeyBindingManager.getKeyBindingManager().set("left", KeyInput.KEY_LEFT);
		KeyBindingManager.getKeyBindingManager().set("right", KeyInput.KEY_RIGHT);
		KeyBindingManager.getKeyBindingManager().set("enter", KeyInput.KEY_RETURN);
		KeyBindingManager.getKeyBindingManager().set("exit", KeyInput.KEY_ESCAPE);
		menus = new ArrayList<Text>();    
		menu1.print("Rotation speed: "+(int)TestGameStateSystem.rotateSpeed/10);
		menu2.print("Walk speed: "+(int)TestGameStateSystem.walkSpeed);
		menu3.print("Difficulty: n/a ");
		menu4.print("Key1: n/a");
		menu5.print("Key2: n/a");
		menus.add(menu1);
		menus.add(menu2);
		menus.add(menu3);
		menus.add(menu4);
		menus.add(menu5);   
	}

	protected void stateUpdate(float tpf) {

		for (int i=0;i<menus.size();i++) { 
			if (selected == i) {
				Text casket=menus.get(i);
				casket.setTextColor(ColorRGBA.green);
				casket.getLocalTranslation().set( 
						display.getWidth()/2-100,
						display.getHeight()/2-i*20, 0 );
			}
			else{
				Text casket=menus.get(i);
				casket.setTextColor(ColorRGBA.white);
				casket.getLocalTranslation().set( 
						display.getWidth()/2-100,
						display.getHeight()/2-i*20, 0 );
			}
			rootNode.attachChild( menu1 );
			rootNode.attachChild( menu2 );
			rootNode.attachChild( menu3 );
			rootNode.attachChild( menu4 );
			rootNode.attachChild( menu5 );						
		}
		if (KeyBindingManager.getKeyBindingManager().isValidCommand("up", false)) {
			selected--;
			if (selected <0) {
				selected =  menus.size()-1;
			}
		}
		if (KeyBindingManager.getKeyBindingManager().isValidCommand("down", false)) {
			selected++;
			if (selected >= menus.size()) {
				selected = 0;
			}
		}
		if (KeyBindingManager.getKeyBindingManager().isValidCommand("left", false)) {
			if (selected==0){
			    if (TestGameStateSystem.rotateSpeed>20)
				TestGameStateSystem.rotateSpeed-=10;
			    else TestGameStateSystem.rotateSpeed=10;
				initInput();
			}
			if (selected==1){
			    if (TestGameStateSystem.walkSpeed>1)
				TestGameStateSystem.walkSpeed-=1;
			    else TestGameStateSystem.walkSpeed=1;
				initInput();
			}
		}
		if (KeyBindingManager.getKeyBindingManager().isValidCommand("right", false)) {
			if (selected==0){
			    if (TestGameStateSystem.rotateSpeed<400)
				TestGameStateSystem.rotateSpeed+=10;
			    else TestGameStateSystem.rotateSpeed=400;
				initInput();
			}
			if (selected==1){
			    if (TestGameStateSystem.walkSpeed<16)
				TestGameStateSystem.walkSpeed+=1;
			    else TestGameStateSystem.walkSpeed=16;
				initInput();
			}
		}
		if (KeyBindingManager.getKeyBindingManager().isValidCommand("enter", false)) {
			if (selected==0){
			}
			if (selected==1){
			}
			if (selected==4){
			}
		}
		if (KeyBindingManager.getKeyBindingManager().isValidCommand("exit", false)) {
			GameState title = new MenuState("menu");
			title.setActive(true);
			GameStateManager.getInstance().attachChild(title);
			this.setActive(false);
		}
		rootNode.updateRenderState();
	}    
}